//
//  SetViewController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "HandleNetLogin.h"
#import "MyAccountCell.h"
#import "CKPuslichTool.h"
#import "TKAlertCenter.h"
#import "RegularViewController.h"
#import "MyInfoController.h"
#import "SetViewController.h"
#import "ChangePassController.h"
#import "MyMessageController.h"
#import "myWebViewController.h"
#import "UserModel.h"
#import "AppDelegate.h"
#import "NSString+NN.h"
#import "UIAlertView+ActionBlock.h"
#import "MyNavigationController.h"
#import "LoginViewController.h"
#import "HandleNetProduct.h"
#import "HelpCenterController.h"
#import "RegisterTextController.h"
#import "NetworkConfig.h"
#import "PersonalViewController.h"
#import "Header.h"

@interface SetViewController (){
    BOOL isOffHand;//是临时客户
    NSString *teacherName ;//理财师名字
    NSString *myType ;//风险等级
    NSString *telephone ;//400电话
    
    NSString *shim;
    NSString *heg;
    NSString *fengx;
}
@property (weak, nonatomic) IBOutlet UITableView *myTable;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UIButton *logoutBt;
@property (weak, nonatomic) IBOutlet UIView *logoutView;
@property (strong,nonatomic) NSArray *DataArray;
@property (strong, nonatomic) UserModel *userMode;
@end
static BOOL isReLoad=YES;
@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"设置";
    isOffHand=NO;
    self.logoutBt.layer.cornerRadius=5;
    telephone=@"400-1681-235";
    shim = @"";
    heg = @"";
    fengx = @"";
    teacherName=@"";
    myType=@"";
    _userMode=[UserModel getUser];
    self.userName.text=_userMode.CustomerName;
    if ([_userMode.Accountstatus intValue]==1) {
        //正式客户
        self.DataArray=@[@{@"image":@"tishi",@"name":@"信息提示",@"type":@"0"},
                         @{@"image":@"mima-1",@"name":@"修改密码",@"type":@"1"},
                         @{@"image":@"fengxian",@"name":@"风险评测",@"type":@"4"},
                         @{@"image":@"kefu",@"name":@"客服电话",@"type":@"5"},
                         @{@"image":@"falv",@"name":@"法律声明",@"type":@"6"},
                         @{@"image":@"help",@"name":@"帮助中心",@"type":@"7"},
                         @{@"image":@"gengxin",@"name":@"检测更新",@"type":@"8"}];
        isOffHand=NO;
    }else{
        //临时客户
        self.DataArray=@[@{@"image":@"tishi",@"name":@"信息提示",@"type":@"0"},
                         @{@"image":@"mima-1",@"name":@"修改密码",@"type":@"1"},
                         @{@"image":@"icon_2",@"name":@"实名认证",@"type":@"2"},
                         @{@"image":@"fengxian",@"name":@"风险评测",@"type":@"4"},
                         @{@"image":@"icon_1",@"name":@"合格投资者认证",@"type":@"3"},
                         @{@"image":@"kefu",@"name":@"客服电话",@"type":@"5"},
                         @{@"image":@"falv",@"name":@"法律声明",@"type":@"6"},
                         @{@"image":@"help",@"name":@"帮助中心",@"type":@"7"},
                         @{@"image":@"gengxin",@"name":@"检测更新",@"type":@"8"}];
        isOffHand=YES;
    }

   
}

- (void)getConfirmStatus{
    [HandleNetProduct getUserIDConfirmStatusForAccountPhone:_userMode.Phone Success:^(NSDictionary *data) {
        if ([data[@"IDConfirm"] isEqualToString:@"完成"]) {
            shim= @"已认证";
        }else{
            shim= @"未认证";
        }
        if ([data[@"InvestorConfirm"] isEqualToString:@"完成"]) {
            heg= @"已认证";
        }else{
            heg= @"未认证";
        }
        if ([data[@"QuestionnaireFinish"] isEqualToString:@"完成"]) {
            fengx= @"已评测";
        }else{
            fengx= @"未评测";
        }
        [_myTable reloadData];
    } Failure:^(NSString *errorInfo) {
        
    }];
}
-(void)getMySomeInfo{
    
    int mid=[_userMode.CustomerID intValue];
    NSDictionary *params=@{@"AAAMethodName":@"BasicData",@"CustomerId":@(mid),@"Accountstatus":_userMode.Accountstatus};
    [HandleNetProduct getListforParams:params Success:^(NSArray *data) {
        if (data.count >0) {
            NSDictionary *info=data[0];
            telephone=info[@"CustomerServicePhone"];
            teacherName=info[@"UserName"];
            myType=info[@"MatchingDegree"];
            [self.myTable reloadData];
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (isReLoad) {
        [self getConfirmStatus];
        [self reloadUserInfo];
        
        
        
        
    }else{
        isReLoad=YES;
    }
    
}

- (void)reloadUserInfo{
    NSString *pass = [[NSUserDefaults standardUserDefaults] objectForKey:@"PASSCODE"];
    NSString *phone = _userMode.Phone;
    [HandleNetLogin CheckLoginForPhone:phone Password:pass isShow:NO Success:^(NSDictionary *data) {
        NSMutableDictionary *infoData=[[NSMutableDictionary alloc] initWithDictionary:data];
        [infoData setObject:phone forKey:@"phone"];
        UserModel *model = [UserModel userInfo:infoData];
        [model save];
        _userMode = model;
        self.userName.text=model.CustomerName;
        if ([_userMode.Accountstatus intValue]==1) {
            //正式客户
            self.DataArray=@[@{@"image":@"tishi",@"name":@"信息提示",@"type":@"0"},
                             @{@"image":@"mima-1",@"name":@"修改密码",@"type":@"1"},
                             @{@"image":@"fengxian",@"name":@"风险评测",@"type":@"4"},
                             @{@"image":@"kefu",@"name":@"客服电话",@"type":@"5"},
                             @{@"image":@"falv",@"name":@"法律声明",@"type":@"6"},
                             @{@"image":@"help",@"name":@"帮助中心",@"type":@"7"},
                             @{@"image":@"gengxin",@"name":@"检测更新",@"type":@"8"}];
            isOffHand=NO;
        }

        [self getMySomeInfo];
    } Failure:^(NSString *errorInfo) {
        [self getMySomeInfo];
    }];
     
       
        
}

- (IBAction)goMyInfomationController:(id)sender {
    MyInfoController *vc=(MyInfoController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"MyInfoController"];
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)logoutBtPress:(UIButton *)sender {
    if (sender.tag==1) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USERISLOGINKEY"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        //    NSDictionary *dic=@{@"index":@"0"};
        //    [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
        AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        
        
        
        LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
        MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
        tempAppDelegate.window.rootViewController=nav;

    }
    self.logoutView.hidden=YES;
}


- (IBAction)logout:(id)sender {
//    self.logoutView.hidden=NO;
    
    NSString *msg = @"是否退出登录？";
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
    alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
        if (buttonIndex == 1) {
            //退出
             [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PICPASSCODE"];
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USERISLOGINKEY"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            //    NSDictionary *dic=@{@"index":@"0"};
            //    [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
            AppDelegate *tempAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            
            
            
            LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
            MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
            tempAppDelegate.window.rootViewController=nav;
        }
        
    };
    [alert show];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)CheckAPPVerson{
//    [HandleNetProduct updateAPPVersonSuccess:^(NSDictionary *data) {
//
//    } Failure:^(NSString *errorInfo) {
//
//    }];
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id984909671?mt=8"]];
//    return;
    
    //AAAMethodName=UpdateAPPEdition&APPEdition=1.0&Device=IOS
    //AAAMethodName=SelectAPPEdition&Device=IOS
    
    [HandleNetProduct CheckAPPVersonSuccess:^(NSDictionary *data) {
        if ([data[@"data"] count] == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您当前已经是最新版本了" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
                if (buttonIndex == 1) {
                    //
                    
                }
                
            };
            [alert show];
            return ;
        }
        
        NSMutableString *version = [NSMutableString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
        NSInteger appNum = [version changeToInteger];
        
        
        NSArray *dda =data[@"data"];
        NSDictionary *dicct = dda[0];
        NSInteger maxNum = [dicct[@"APPEdition"] changeToInteger];
//        NSLog(@"version---%@  getAPPversion--%@ ",version,getAPPversion);
        if (maxNum>appNum) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"检测到新版本，是否更新？" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
            alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
                if (buttonIndex == 0) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id984909671?mt=8"]];
                    
                }
                
            };
            [alert show];
        }else{
           
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您当前已经是最新版本了" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
                if (buttonIndex == 1) {
                    //
                    
                }
                
            };
            [alert show];
        }
    } Failure:^(NSString *errorInfo) {
        
    }];

    
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50
    ;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if (isOffHand) {
        return 2;
    }else{
        return 3;
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (isOffHand) {
        if (section==0) {
            return self.DataArray.count;
        }
        return 1;
    }else{
        if (section==1) {
            return self.DataArray.count;
        }
        return 1;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isOffHand) {
        if (indexPath.section==0) {
            MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryNone;
            NSDictionary *info=self.DataArray[indexPath.row];
            cell.mImage.image=[UIImage imageNamed:info[@"image"]];
            cell.NameLabel.text=info[@"name"];
            cell.RightLabel.text=@"";
            cell.jiantou.hidden = NO;
            if ([info[@"type"] intValue]==2) {
                cell.RightLabel.text=shim;//实名认证
                if ([shim isEqualToString:@"已认证"]) {
                    cell.jiantou.hidden = YES;
                }
            }else if ([info[@"type"] intValue]==3) {
                cell.RightLabel.text=heg;//合格投资者认证
                if ([heg isEqualToString:@"已认证"]) {
                    cell.jiantou.hidden = YES;
                }
            }else if ([info[@"type"] intValue]==4) {
                cell.RightLabel.text=fengx;//我的风险评测等级
                if ([fengx isEqualToString:@"已评测"]) {
                    cell.RightLabel.text = myType;
                }
            }else if ([info[@"type"] intValue] == 5) {
                cell.RightLabel.text=telephone;
            }
            if ([info[@"type"] intValue]==8) {
                cell.RightLabel.text=[NSString stringWithFormat:@"V%@",DEF_PERSISTENT_GET_OBJECT(@"showGuideVersion")];
            }
            return cell;
        }else{
            MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryNone;
            cell.mImage.image=[UIImage imageNamed:@"lianxi"];
            cell.NameLabel.text=@"联系我们";
            cell.RightLabel.text=@"";
            return cell;
        }
    }else{
        if (indexPath.section==0) {
            MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell0" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryNone;
            cell.mImage.image=[UIImage imageNamed:@"licai"];
            cell.NameLabel.text=@"我的理财师";
            cell.RightLabel.text=teacherName;
            return cell;
        }else if (indexPath.section==1){
            MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryNone;
            NSDictionary *info=self.DataArray[indexPath.row];
            cell.mImage.image=[UIImage imageNamed:info[@"image"]];
            cell.NameLabel.text=info[@"name"];
            cell.RightLabel.text=@"";
            if ([info[@"type"] intValue]==4) {
                cell.RightLabel.text=myType;//我的风险评测等级
            }else if ([info[@"type"] intValue]==5) {
                cell.RightLabel.text=telephone;
            }
            if ([info[@"type"] intValue]==8) {
                cell.RightLabel.text=[NSString stringWithFormat:@"V%@",DEF_PERSISTENT_GET_OBJECT(@"showGuideVersion")];
            }
            return cell;
        }else{
            MyAccountCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MyAccountCell" forIndexPath:indexPath];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryNone;
            cell.mImage.image=[UIImage imageNamed:@"lianxi"];
            cell.NameLabel.text=@"联系我们";
            cell.RightLabel.text=@"";
            return cell;
        }

    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    isReLoad=NO;
    NSDictionary *info=self.DataArray[indexPath.row];
    
    if (isOffHand) {
        if (indexPath.section==0) {
            switch ([info[@"type"] intValue]) {
                case 0://信息提示
                {
                    MyMessageController *vc=(MyMessageController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"MyMessageController"];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
                    break;
                case 1://修改密码
                {
                    ChangePassController *vc=(ChangePassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"ChangePassController"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2://实名认证
                {
                    
                    
                    if ([shim isEqualToString:@"已认证"]) {
                        return;
                    }
                    
                    PersonalViewController *vc=(PersonalViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"PersonalViewController"];
                    isReLoad = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 3://合格投资者认证
                {
                    
                    if ([shim isEqualToString:@"未认证"]) {
                        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请先实名认证"];
                        return;
                    }
                    if ([fengx isEqualToString:@"未评测"]) {
                        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请先风险评测"];
                        return;
                    }
                    
                    if ([heg isEqualToString:@"已认证"]) {
                        return;
                    }
                    RegularViewController *vc=(RegularViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"RegularViewController"];
                    isReLoad = YES;
                    [vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
                    [vc setModalPresentationStyle:UIModalPresentationOverFullScreen];
                    vc.returnBlock = ^{
                        heg = @"已认证";
                        [self.myTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    };
                    [self presentViewController:vc animated:YES completion:nil];
                   
                }
                    break;
                    
                case 4://风险评测
                {
//                    if ([fengx isEqualToString:@"已评测"]) {
//                        return;
//                    }
                    if ([shim isEqualToString:@"未认证"]) {
                        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请先实名认证"];
                        return;
                    }
                    myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
                    vc.title=@"风险评测";
                    UserModel *mode=[UserModel getUser];
                    if (myType.length==0) {
                        vc.urlString=[NSString stringWithFormat:@"%@?CustomerId=%@&MobilePhone=%@",API_FXPC,mode.CustomerID,mode.Phone];
                    }else{
                        vc.urlString=[NSString stringWithFormat:@"%@?CustomerId=%@&Accountstatus=%@",API_FXPCJG,mode.CustomerID,mode.Accountstatus];
                    }
                    isReLoad=YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                
                case 5://客服电话
                {
                    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",telephone];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                }
                    break;
                case 6://法律声明
                {
                    myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
                    vc.title=@"法律声明";
                    
                    vc.urlString=API_FLSM;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 7://帮助中心
                {
                    HelpCenterController *vc=(HelpCenterController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"HelpCenterController"];
                    vc.title=@"帮助中心";
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 8://检测更新
                {
                    [self CheckAPPVerson];
                }
                    break;
                default:
                    break;
            }
        }else if (indexPath.section==1){
            //联系我们
            myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
            vc.title=@"联系我们";
            vc.urlString=API_CONTACT_US;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        if (indexPath.section==1) {
            switch ([info[@"type"]intValue]) {
                case 0://信息提示
                {
                    MyMessageController *vc=(MyMessageController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"MyMessageController"];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
                    break;
                case 1://修改密码
                {
                    ChangePassController *vc=(ChangePassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"ChangePassController"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 4://风险评测
                {
                    myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
                    vc.title=@"风险评测";
                    UserModel *mode=[UserModel getUser];
                    if (myType.length>0) {
                        vc.urlString=[NSString stringWithFormat:@"%@?CustomerId=%@&Accountstatus=%@",API_FXPCJG,mode.CustomerID,mode.Accountstatus];
                    }else{
                        vc.urlString=[NSString stringWithFormat:@"%@?CustomerId=%@&MobilePhone=%@",API_FXPC,mode.CustomerID,mode.Phone];
                    }
                    
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 5://客服电话
                {
                    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",telephone];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                }
                    break;
                case 6://法律声明
                {
                    myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
                    vc.title=@"法律声明";
                    vc.urlString=API_FLSM;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 7://帮助中心
                {
                    HelpCenterController *vc=(HelpCenterController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"HelpCenterController"];
                    vc.title=@"帮助中心";
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 8://检测更新
                {
                    [self CheckAPPVerson];
                }
                    break;
                default:
                    break;
            }
        }else if (indexPath.section==2){
            //联系我们
            myWebViewController *vc=(myWebViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"myWebViewController"];
            vc.title=@"联系我们";
            vc.urlString=API_CONTACT_US;
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }
    }
    
    
}

@end
